package rxjava.application.com.androidmyp.Data;

import rx.Observable;
import rx.Subscriber;
import rxjava.application.com.androidmyp.Beans.BaseBean;

/**
 * @author Qiujc
 * created time 2016/5/10
 * 过滤器，把一些错误的网络请求直接抛出
 */
public class ApiInterceptor {
    public <T> Observable<T> flatResult(final BaseBean<T> result) {
        return Observable.create(
                new Observable.OnSubscribe<T>() {
                    @Override
                    public void call(Subscriber<? super T> subscriber) {
                        switch (result.getRespCode()) {
                            case "10000":
                                subscriber.onNext(result.getData());
                                break;
                            case "10003":
                                subscriber.onError(new ApiException(result.getRespCode(), result.getRespMsg()));
                                break;
                            default:
                                subscriber.onError(new ApiException(result.getRespCode(), result.getRespMsg()));
                                break;
                        }
                        subscriber.onCompleted();
                    }
                });
    }
}
